package com.noreply.springlearning.async;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/**
 * @author noreply
 * @description
 * @datetime 2022/5/5 14:52
 */
@Service
public class TaskService {

    //@Async("taskExecutor")
    @Async
    public void runTask1() {
        System.out.println("task1 start");
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("task1 finish");
    }

   // @Async("taskExecutor")
   @Async
    public void runTask2() {
        System.out.println("task2 start");
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("task2 finish");
    }

    @Async
    public void runTaskException() {
        System.out.println("runTaskException start");
        int a = 10 / 0;
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("runTaskException finish");
    }

    @Async
    public Future<String> result() {
        System.out.println("result start");
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("result finish");
        return new AsyncResult<>("hello async");
    }

}
